import {
    createSlice,
    createAsyncThunk
} from "@reduxjs/toolkit"
 
import {
    movieRequest
} from '../../api/index'
//我们一般使用createAsyncThunk方法来创建异步action
//createAsyncThunk接受两个参数，第一个参数时type的值，第二个参数时回调函数，回调函数就是异步的action函数
 export const getmovie = createAsyncThunk('movie/getMovieList',async ()=>{
    const result = await movieRequest()
    //拿到result
    return  result.data.data.list
})

 const movieSlice =createSlice({
    name:'movieList',
    initialState:{
        movie:[]
    },
    reducers:{

    },
    //因为reducers中会重新创造action所以一般异步代码要写在额外的reducer中
    extraReducers:{
        [getmovie.fulfilled](prev,action){
            console.log("异步action成功--getMovieAction");
                prev.movie = action.payload
        },
        [getmovie.rejected](){
            console.log("异步action失败--getMovieAction");
        },
        [getmovie.pending](){
            console.log("异步action加载中--getMovieAction");
        },
    }
})
export default movieSlice.reducer
